package com.ujiuye.servlet;

import com.ujiuye.entity.Course;
import com.ujiuye.entity.CourseUser;
import com.ujiuye.entity.ResultVO;
import com.ujiuye.service.CourseUserService;
import com.ujiuye.service.impl.CourseUserServiceImpl;
import com.ujiuye.util.JsonUtils;
import com.ujiuye.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/cs")
public class CourseUserServlet extends BaseServlet {
    CourseUserService cus= new CourseUserServiceImpl();
    protected void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        //获取页容量
        String pageSize = req.getParameter("pageSize");
        //调用service 返回PageBean对象
       PageBean<CourseUser> pageBean = cus.findByPage(currentPage,pageSize);
       //将pageBean对象转化为json对象
        String json = JsonUtils.toJson(pageBean, resp);
       //将json传到前端
        resp.getWriter().print(json);

    }

    protected void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                 //获取ids
        String ids = req.getParameter("ids");
        //调用service
        int num = cus.delAll(ids);
        //给出响应
        ResultVO vo =null;
        if (num>0){
            vo = new ResultVO(200,"删除成功",null);
        }else {
            vo = new ResultVO(200,"删除失败",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);

    }
    //查询所有课程
    protected void findAllCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //调用service
        List<Course> list = cus.findAllCourse();
        //给出响应
        ResultVO vo =null;
        if (list!=null){
            vo = new ResultVO(200,"查询成功",list);
        }else {
            vo = new ResultVO(200,"查询失败",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);

    }
    //根据id查询所有课程
    protected void updateCourseById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cid和id
        int id = Integer.parseInt(req.getParameter("id"));
        int cid = Integer.parseInt(req.getParameter("cid"));
        //调用service
        int num = cus.updateCourseById(id,cid);


        //给出响应
        ResultVO vo =null;
        if (num>0){
            vo = new ResultVO(200,"修改成功",null);
        }else {
            vo = new ResultVO(200,"修改失败",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);

    }

    //购买课程
    //uqlkks2134@sandbox.com
    protected void insertCourseUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取uid和cid
        int uid = Integer.parseInt(req.getParameter("uid"));
        int cid = Integer.parseInt(req.getParameter("cid"));

        //调用service
        int num = cus.insertCourseUser(uid,cid);

        resp.sendRedirect("http://127.0.0.1:5500/pages/course.html");


    }

    //购买课程
    //uqlkks2134@sandbox.com
    protected void findCourseUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取uid和cid
        int uid = Integer.parseInt(req.getParameter("uid"));
        int cid = Integer.parseInt(req.getParameter("cid"));

        //调用service
        CourseUser cu = cus.findCourseUser(uid,cid);

        //给出响应
        ResultVO vo =null;
        if (cu!=null){
            vo = new ResultVO(200,"已购买课程",null);
        }else {
            vo = new ResultVO(500,"未购买课程",null);
        }
        String json = JsonUtils.toJson(vo, resp);
        resp.getWriter().print(json);

    }




}
